#include "head.h"
#include <stdlib.h>


int create_salt( char*salt) {//创建盐值
    const char charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    srand(time(NULL));  // 初始化随机种子（不够安全！）
    strcpy(salt, "$6$");
    for (int i = 3; i < 16; i++) {
        salt[i] = charset[rand() % (sizeof(charset) - 1)];
    }
    salt[16] = '\0';

    printf("salt=%s\n",salt);
    return 0;
}






//z注册函数
int register_user(int socket, int *success,char*token,char*username){
    TLV*TLV_T=(TLV*)malloc(sizeof(TLV));
    TLV*TLV_RECV=(TLV*)malloc(sizeof(TLV));
    memset(TLV_RECV,0,sizeof(TLV));
int id=0;
    memset(TLV_T,0,sizeof(TLV));
    TLV_T->type=CMD_REGISTER;
    printf("注册开始，请输入用户名\n");

    char buf[9];
    char buf1[9];
    memset(buf,0,9);
    memset(buf1,0,9);
    char c;
    while ((c = getchar()) != '\n' && c != EOF);
    if (fgets(buf, sizeof(buf), stdin)) {
        // 去除可能的换行符
        printf("要求：输入的用户名和密码均为8位\n");
        buf[strcspn(buf, "\n")] = '\0';
        printf("你输入的用户名是%s\n", buf);
    } else {
        printf("读取输入失败\n");
    }
    buf[8]='\0';
    int num_username=strlen(buf);
    if(num_username!=8){
        printf("用户名不足八位\n");
        return -1;
    }
    TLV_T->length=16;
    printf("准备发送密码\n");
    while ((c = getchar()) != '\n' && c != EOF);
    if (fgets(buf1, sizeof(buf1), stdin)) {
        // 去除可能的换行符
        buf1[8] = '\0'; 
        printf("你输入的密码是%s\n", buf1);
    } else {
        printf("读取输入失败\n");
    }

    for (int i = 0; i < 8; i++) {
        TLV_T->value[i] = buf[i];
        TLV_T->value[i + 8] = buf1[i];
    }
    printf("TLV_T->value=%s\n",TLV_T->value);
  while ((c = getchar()) != '\n' && c != EOF);


    sendTlv(socket,TLV_T);
    recvTlv(socket,TLV_RECV);
    if(TLV_RECV->type==0xFF){
        printf("注册失败\n");
    }
    else{
strncpy(token,TLV_RECV->token,65);
        id=TLV_RECV->id;
         printf("user_id=%d,token=%s\n",id,token);
        printf("success\n");
        *success= 1;
 id=TLV_RECV->id;
   strncpy(token,TLV_RECV->token,65);
    printf("user_id=%d,token=%s\n",id,token);
    strncpy(username,TLV_RECV->username,9);
    }
    free(TLV_RECV);
    free(TLV_T);
    return id;
};

int  is_absolute_path(const char* path){
    if (path == NULL || *path == '\0') {
        return 0;
    }

    // 绝对路径的第一个字符是'/'
    if (path[0] == '/')
        return 1;
    else return 0;


}
int init_train(TLV* TLV_t){
    TLV_t->length = 0;
    TLV_t->type = 0xFF;
    TLV_t->id = 0;	memset(TLV_t->value, 0, sizeof(TLV_t->value));
memset(TLV_t->username,0,9);
memset(TLV_t->token,0,65);
return 0;


}

